package com.tbb.app.base.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.tbb.app.base.search.NotePageSearch;
import com.tbb.app.base.search.UserLoginSearch;
import com.tbb.app.base.search.UserPageSearch;
import com.tbb.app.base.service.IFollowService;
import com.tbb.app.base.service.INoteService;
import com.tbb.app.base.service.IUserNoteService;
import com.tbb.app.base.service.IUserService;
import com.tbb.app.base.vo.NoteVO;
import com.tbb.app.base.vo.UserVO;
import com.tbb.app.common.entity.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/mfh")
@Slf4j
@ApiSupport(order = 4)
@Api(tags = "孟傅红相关接口")
public class MFHController {

    @Autowired
    private IUserService iUserService;
    @Autowired
    private IFollowService iFollowService;
    @Autowired
    private INoteService iNoteService;
    @Autowired
    private IUserNoteService iUserNoteService;

    @PostMapping("/user/login")
    @ApiOperation(value = "登入用户")
    @ApiOperationSupport(order = 1)
//    @CrossOrigin(origins = "*")
    public BaseResponse<String> login(
            @RequestBody
            @Validated
                    UserLoginSearch search) {
        return BaseResponse.data(iUserService.login(search.getAccount(), search.getPassword()));
    }

    @GetMapping("/user/search/list")
    @ApiOperation(value = "列表获取当前用户搜索历史")
    @ApiOperationSupport(order = 2)
    public BaseResponse<List<String>> listSearch() {
        return BaseResponse.data(iUserService.listSearch());
    }

    @GetMapping("/user/search/delete")
    @ApiOperation(value = "清空当前用户搜索历史")
    @ApiOperationSupport(order = 3)
    public BaseResponse<Object> deleteSearch() {
        iUserService.deleteSearch();
        return BaseResponse.ok();
    }

    @PostMapping("/note/page")
    @ApiOperation(value = "根据关键字分页查询笔记")
    @ApiOperationSupport(order = 4)
    public BaseResponse<IPage<NoteVO>> pageNote(
            @RequestBody
            @Validated
                    NotePageSearch search) {
        return BaseResponse.data(iNoteService.page(search, true));
    }

    @GetMapping("/note/like")
    @ApiOperation(value = "点赞笔记")
    @ApiOperationSupport(order = 5)
    public BaseResponse<Object> likeNote(
            @RequestParam
            @ApiParam(value = "笔记id", required = true)
                    String noteId,
            @RequestParam
            @ApiParam(value = "操作类型，必填，0-取消点赞，1-点赞", required = true)
                    int type) {
        iUserNoteService.like(noteId, type);
        return BaseResponse.ok();
    }

    @PostMapping("/user/page")
    @ApiOperation(value = "根据关键字分页查询用户")
    @ApiOperationSupport(order = 6)
    public BaseResponse<IPage<UserVO>> pageUser(
            @RequestBody
            @Validated
                    UserPageSearch search) {
        return BaseResponse.data(iUserService.page(search, true));
    }

    @GetMapping("/user/follow")
    @ApiOperation(value = "关注用户")
    @ApiOperationSupport(order = 7)
    public BaseResponse<Object> followUser(
            @RequestParam
            @ApiParam(value = "被关注用户id，必填", required = true)
                    String followedId,
            @RequestParam
            @ApiParam(value = "操作类型，必填，0-取消关注，1-关注", required = true)
                    int type) {
        iFollowService.follow(followedId, type);
        return BaseResponse.ok();
    }


}
